package com.cortex.papaya.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.cortex.papaya.listener.SlidViewListener;
import com.cortex.papaya.listener.SlidViewListener.OnSlidFingerListener;
import com.cortex.papaya.listener.SlidViewListener.SlidDirection;
import com.cortex.papaya.search.Link;
import com.cortex.papayasearch.R;

public class LinkView extends RelativeLayout implements  OnSlidFingerListener{

	private TextView tvDocumentName,tvDocumentUrl,tvDocumentSummary;
	private ImageView imgIcon;
	private FrameLayout containerDocument;
	private ScrollView scrollDocumentSummary;
	private ViewFlipper flipperDocument;
	private Link document;
	private Animation in;
	private Animation out;
	private OnClickDocumentView onClickView;

	public LinkView(Context context) {
		super(context);
	}
	public LinkView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public LinkView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}
	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();
		initViews();
		initHandlers();
		
	}
	private void initHandlers(){
		// enable clicking when document show name
		flipperDocument.setOnTouchListener(new SlidViewListener(10, this, SlidDirection.LEFT,SlidDirection.RIGHT,SlidDirection.CLICK));
		// enable clicking when document show summary
		scrollDocumentSummary.setOnTouchListener(new SlidViewListener(10, this, SlidDirection.LEFT,SlidDirection.RIGHT,SlidDirection.CLICK));
	}
	
	private void initViews(){
		tvDocumentName = (TextView)findViewById(R.id.tvDocumentName);
		tvDocumentUrl = (TextView)findViewById(R.id.tvDocumentUrl);
		tvDocumentSummary = (TextView)findViewById(R.id.tvDocumentSummary);
		imgIcon = (ImageView)findViewById(R.id.imgIcon);
		containerDocument = (FrameLayout)findViewById(R.id.containerDocument);
		scrollDocumentSummary = (ScrollView)findViewById(R.id.scrollDocumentSummary);
		flipperDocument = (ViewFlipper)findViewById(R.id.flipperDocument);
	}

	public Link getDocument() {
		return document;
	}
	public void setDocument(Link document) {
		this.document = document;
		tvDocumentName.setText(document.getName());
		tvDocumentUrl.setText(document.getSummaryUrl());
		tvDocumentSummary.setText(document.getSummary());
		imgIcon.setImageBitmap(document.getImage());
	}
	public void setIconSize(int width,int height){
		FrameLayout.LayoutParams params = (android.widget.FrameLayout.LayoutParams) imgIcon.getLayoutParams();
		params.width = width;
		params.height = height;
		imgIcon.setLayoutParams(params);
	}
	public void setOnClickViewListener(OnClickDocumentView onClickView){
		this.onClickView = onClickView;
	}
	@Override
	public void startedSlid() {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void finishedSlid(SlidDirection slidDirection) {
		if(slidDirection == SlidDirection.LEFT){
			moveStateView( R.anim.push_left_in, R.anim.push_left_out);
		}
		else if(slidDirection == SlidDirection.RIGHT){
			moveStateView( R.anim.push_right_in, R.anim.push_right_out);
		}
		else if(slidDirection == SlidDirection.CLICK){
			if(onClickView != null){
				onClickView.onClick(this);
			}
		}
	}
	private void moveStateView(int animationIn,int anumationOut){
		View curView = flipperDocument.getCurrentView();
		LinkView me = LinkView.this;
		Context context = me.getContext();
		in = AnimationUtils.loadAnimation(context, animationIn);
		out = AnimationUtils.loadAnimation(context, anumationOut);
		flipperDocument.setInAnimation(in);
		flipperDocument.setOutAnimation(out);
		if(curView == containerDocument){
			flipperDocument.showNext();	
		}
		else{
			flipperDocument.showPrevious();
		}
	}
	public static LinkView createDocumentView(Context context,Link document){

		LayoutInflater inflter = LayoutInflater.from(context);
		LinkView rlDocument = (LinkView)inflter.inflate(R.layout.view_link, null);
		
		rlDocument.setDocument(document);

		return rlDocument;
	}

	public interface OnClickDocumentView{
		void onClick(LinkView view);
	}
}
